package net.pm.core.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import net.pm.common.dao.GenericDao;
import net.pm.common.service.impl.GenericServicerImpl;
import net.pm.common.toolkit.PMTool;
import net.pm.common.toolkit.date.DateUtils;
import net.pm.core.entities.db.Collarmaterial;
import net.pm.core.entities.db.Material;
import net.pm.core.entities.db.Materiallog;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.entities.db.Product;
import net.pm.core.service.CollarMaterialServicer;
import net.pm.core.service.MaterialServicer;
import net.pm.core.service.MateriallogServicer;

import org.springframework.stereotype.Service;

@Service("CollarMaterialServicer")
public class CollarMaterialServicerImpl extends GenericServicerImpl<Collarmaterial, Long> implements CollarMaterialServicer{

	@Resource(name="CollarMaterialDao")
	@Override
	public void setGenericDao(GenericDao<Collarmaterial, Long> genericDao) {
		super.genericDao = genericDao;
	}
	
	@Resource(name="MaterialServicer")
	private MaterialServicer materialServicer;
	@Resource(name="MateriallogServicer")
	private MateriallogServicer materiallogServicer;

	@Override
	public Long createCollarAndLog(Map<Long, Integer> collarmap,
			Long productId, Long userId) {
		Collarmaterial collar = new Collarmaterial();
		collar.setProduct(new Product(productId));
		Long create_retval = this.createCollar(collar);
		if (create_retval.equals(CollarMaterialServicer.create_collar_success)) {
			// 添加物料　领料日志
			List<Materiallog> materiallogs = new ArrayList<Materiallog>(
					collarmap.size());
			Set<Long> keys = collarmap.keySet();
			for (Long key : keys) {
				Integer getNumber = collarmap.get(key);
				Materiallog materiallog = new Materiallog();
				materiallog.setGetNumber(getNumber);
				materiallog.setMaterial(new Material(key));
				materiallog.setOperationTime(DateUtils.obtainCurrentDate());
				materiallog.setPmuser(new Pmuser(userId));
				materiallog.setStatus(Materiallog.status_get);
				materiallog.setCollarmaterial(new Collarmaterial(collar.getId()));
				materiallogs.add(materiallog);

				// 对应的减少库存
				materialServicer.collarMaterial(key, getNumber);

			}
			// 最后再批量记录领取物料日志　
			materiallogServicer.batchCreate(materiallogs);
			return CollarMaterialServicer.create_collarandlog_success;
		}
		return CollarMaterialServicer.create_collarandlog_failed;
	}

	@Override
	public Long createCollar(Collarmaterial collar) {
		//setter 一些默认属性
		collar.setAddTime(DateUtils.obtainCurrentDate());
		collar.setIsBack(false);
		Long id = super.create(collar);
		if(PMTool.isNotEmpty(id))
			return CollarMaterialServicer.create_collar_success;
		return CollarMaterialServicer.create_collar_failed;
	}

	@Override
	public List<Long> findCollarIdsByProductId(Long productId) {
		final StringBuilder builder = new StringBuilder(" from Collarmaterial collar where collar.product.id = ? ");
		List<Collarmaterial> collars = super.findEntitiesByJpql(builder, productId);
		List<Long> collarIds = new ArrayList<Long>(collars.size());
		for(Collarmaterial collar : collars){
			collarIds.add(collar.getId());
		}
		return collarIds;
	}

}
